// pages/withdrawal/withdrawal.js
const api = require('../../utils/http.js');
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		balance: null,
		withdrawalMoney: ''
	},
	//全部提现
	allWithdrawal() {
		this.setData({
			withdrawalMoney: this.data.balance
		})
	},
	//提交
	withdrawalFormSubmit(e) {
    console.log(e.detail.value, this.data.balance)
		let withdrawalInfo = e.detail.value
		if (!withdrawalInfo.name.trim()) {
			wx.showToast({
				title: '请输入姓名',
				icon: 'none'
			})
		} else if (!withdrawalInfo.account.trim()) {
			wx.showToast({
				title: '请输入账号',
				icon: 'none'
			})
		} else if (!withdrawalInfo.confirmAccount.trim()) {
			wx.showToast({
				title: '请确认账号',
				icon: 'none'
			})
		}else if (withdrawalInfo.confirmAccount.trim() != withdrawalInfo.account.trim()) {
			wx.showToast({
				title: '您输入的账号不一致',
				icon: 'none'
			})
		} else if (!withdrawalInfo.withdrawalMoney.trim()) {
			wx.showToast({
				title: '请输入提现金额',
				icon: 'none'
			})
    } else if (this.data.balance<100) {
      wx.showToast({
        title: '最低100提现',
        icon: 'none'
      })
      return
    } else {
			let num = parseInt(withdrawalInfo.withdrawalMoney)
			let balance = parseInt(this.data.balance);
      let pay_type = this.data.pay_type == 1 ? "支付宝" : this.data.pay_type == 2 ? "微信钱包" : this.data.pay_type == 3 ? "银行卡" : this.data.pay_type == 4 ? "QQ钱包":"";
			console.log(num,balance)
			if (num > 0 && num <= balance) {
				let uid = app.globalData.uid;
				api.fetchApi('procedure_t', {
          p: `R2008001|${uid}|${withdrawalInfo.withdrawalMoney}|${pay_type}|${withdrawalInfo.account}|${withdrawalInfo.confirmAccount}|${withdrawalInfo.name}`
				}).then(res => {
					const {
						code,
						results
					} = res.data
					if (code == 1) {
						wx.showToast({
							title: '提现成功',
							icon: 'success'
						})
            setTimeout(()=>{
              wx.navigateBack({
                
              })
            },1500)
					} else {
						wx.showToast({
							title: '提现失败',
							icon: 'none'
						})
					}
				})
			} else{
				wx.showToast({
					title: '提现金额小于零或者可提现金额不足',
					icon: 'none'
				})
			}
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			balance: options.balance,
      pay_type: options.pay_type
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
